//
//  KotnetLogout.m
//  KotnetAutologin
//
//	logout from kotnet
//
//  Created by Kristof Marien on 19/01/10.
//  Copyright 2010 KotnetAutologin. All rights reserved.
//

#import "KotnetLogout.h"


@implementation KotnetLogout

@synthesize delegate;
@synthesize cannotLogoutCallback;
@synthesize loggedoutCallback;

/**
 *  starts loggin out
 */
- (void) logout:(id)logoutDelegate cannotLogoutSelector:(SEL)cannotLogoutSelector loggedoutSelector:(SEL)loggedoutSelector
{
	self.delegate = logoutDelegate;
	self.cannotLogoutCallback = cannotLogoutSelector;
	self.loggedoutCallback = loggedoutSelector;
	
	NSString *user = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"userName"];
	KeyChain *keychain = [KeyChain alloc];
	NSString *pwd = [keychain getPasswordKeyChain:user];
	[keychain release];
	
	if (pwd == @"not found PWD") {
		if (self.delegate && self.cannotLogoutCallback) {
			if ([self.delegate respondsToSelector:self.cannotLogoutCallback]) {
				[self.delegate performSelector:self.cannotLogoutCallback withObject:@"Geen gebruiker ingesteld"];
			}
		}
		return;
	}
	
	source = [GetHTMLSource alloc];
	source.url = @"https://netlogin.kuleuven.be/cgi-bin/wayf.pl";
	source.data = [NSString stringWithFormat:@"inout=logout&ip=%@&uid=kuleuven/%@&lang=ned", [[[NSHost currentHost] addresses] objectAtIndex:1],
				   user];
	[source getSource:self receivedSelector:@selector(sourcePWDFieldReceived:) errorSelector:@selector(sourcePWDFieldNotReceived)];
}
/**
 *  source received, extract password field name
 */
- (void)sourcePWDFieldReceived:(NSString *)data
{
	[source release];
	NSString *searchString = @"<input type=\"password\" size=\"20\" maxlength=\"20\" name=\"";
	NSRange range = [data rangeOfString:searchString];
	if (range.location == NSNotFound) {
		[self sourcePWDFieldNotReceived];
		return;
	}
	NSString *user = [[[NSUserDefaultsController sharedUserDefaultsController] values] valueForKey:@"userName"];
	KeyChain *keychain = [KeyChain alloc];
	NSString *pwd;
	do {
		pwd = [keychain getPasswordKeyChain:user];
	} while ([pwd length] < 2);
	[keychain release];
	NSString *pwdField = [data substringFromIndex:range.location+[searchString length]];
	range = [pwdField rangeOfString:@"\""];
	pwdField = [pwdField substringToIndex:range.location];
	
	source = [GetHTMLSource alloc];
	source.url = @"https://netlogin.kuleuven.be/cgi-bin/netlogout.pl";
	source.data = [NSString stringWithFormat:@"inst=kuleuven&lang=nl&ip=%@&uid=%@&%@=%@", [[[NSHost currentHost] addresses] objectAtIndex:1],
				   user, pwdField, pwd];
	[source getSource:self receivedSelector:@selector(logoutSended:) errorSelector:@selector(sourcePWDFieldNotReceived)];
}
/**
 *  error getting source
 */
- (void)sourcePWDFieldNotReceived
{
	[source release];
	if (self.delegate && self.cannotLogoutCallback) {
		if ([self.delegate respondsToSelector:self.cannotLogoutCallback]) {
			[self.delegate performSelector:self.cannotLogoutCallback withObject:@"Onbekende fout"];
		}
	}
}
/**
 *  check source loggin out
 */
- (void)logoutSended:(NSString *)data
{
	[source release];
	NSRange range = [data rangeOfString:@"logout geslaagd."];
	if (range.location == NSNotFound) {
		if (self.delegate && self.cannotLogoutCallback) {
			if ([self.delegate respondsToSelector:self.cannotLogoutCallback]) {
				[self.delegate performSelector:self.cannotLogoutCallback withObject:@"Fout tijdens inloggen"];
			}
		}
	} else {
		if (self.delegate && self.loggedoutCallback) {
			if ([self.delegate respondsToSelector:self.loggedoutCallback]) {
				[self.delegate performSelector:self.loggedoutCallback];
			}
		}
	}
}
@end
